home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Demos / C Demos / Button / Document.c < prev    next >
Text File  |  1995-03-21  |  3KB  |  165 lines

  1. /*
  2.  * The stuff in this file presents a document window with an outlined
  3.  * pushbutton.  It demonstrates:
  4.  * - Button outlining in a document window.
  5.  *
  6.  * Note that button clicks are tracked and return/enter and escape/
  7.  * command-period keyclicks are mapped onto button clicks, but that no
  8.  * other action is associated with those clicks.  This window hander
  9.  * just shows the visible user interface stuff associated with those
  10.  * actions.
  11.  */
  12.  
  13. # include    "TransSkel.h"
  14.  
  15. # include    "Button.h"
  16.  
  17.  
  18. # define    returnKey    13
  19. # define    enterKey    3
  20. # define    escapeKey    27
  21.  
  22.  
  23. static WindowPtr        wind;
  24. static ControlHandle    okBtn;
  25. static ControlHandle    cancelBtn;
  26.  
  27.  
  28. static pascal void
  29. Mouse (Point pt, long t, short mods)
  30. {
  31. ControlHandle    ctrl;
  32. short    partNo;
  33.  
  34.     if ((partNo = FindControl (pt, wind, &ctrl)) != 0)
  35.     {
  36.         /*
  37.          * Check part code.  The constant to use varies.  Universal
  38.          * headers release 2 define a new constant kInButtonControlPart.
  39.          * Release 1 and the old Apple headers use inButton.
  40.          */
  41. #if skelUnivHeaders > 1
  42.         if (partNo == kInButtonControlPart)
  43. #else
  44.         if (partNo == inButton)
  45. #endif
  46.         {
  47.             if (TrackControl (ctrl, pt, nil))
  48.             {
  49.                 /* nothing done here */
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55.  
  56. /*
  57.  * Key handler.  Map return/enter onto clicks in OK button.
  58.  * Note that we check whether the OK button is active or not
  59.  * before flashing the button.  In this application the button
  60.  * is never inactive when the window is active, but that may not
  61.  * be generally true.
  62.  */
  63.  
  64. static pascal void
  65. Key (short c, short code, short mods)
  66. {
  67.     if (c == returnKey || c == enterKey)
  68.     {
  69.         if ((**okBtn).contrlHilite == normalHilite)
  70.             SkelFlashButton (okBtn);
  71.     }
  72.     else if (c == escapeKey || SkelCmdPeriod (SkelGetCurrentEvent ()))
  73.     {
  74.         if ((**cancelBtn).contrlHilite == normalHilite)
  75.             SkelFlashButton (cancelBtn);
  76.     }
  77. }
  78.  
  79.  
  80. /*
  81.  * Update the window.
  82.  */
  83.  
  84. static pascal void
  85. Update (Boolean resized)
  86. {
  87. WindowPtr    wind;
  88. Rect    r;
  89. short    h;
  90.  
  91.     GetPort (&wind);
  92.  
  93.     r = wind->portRect;
  94.     EraseRect (&r);
  95.     DrawControls (wind);
  96.     SkelDrawButtonOutline (okBtn);
  97. }
  98.  
  99.  
  100. /*
  101.  * Make the buttons active or inactive as the window becomes active or
  102.  * inactive.  Redraw default button outline to follow state of default
  103.  * button.
  104.  */
  105.  
  106. static pascal void
  107. Activate (Boolean active)
  108. {
  109. short    hilite;
  110.  
  111.     hilite = (active ? normalHilite : dimHilite);
  112.     HiliteControl (okBtn, hilite);
  113.     SkelDrawButtonOutline (okBtn);
  114.     HiliteControl (cancelBtn, hilite);
  115. }
  116.  
  117.  
  118. static pascal void
  119. Clobber (void)
  120. {
  121. WindowPtr    wind;
  122.  
  123.     GetPort (&wind);
  124.     HideWindow (wind);
  125.     DisposeWindow (wind);
  126. }
  127.  
  128.  
  129. /*
  130.  * Initialize document window
  131.  */
  132.  
  133. void
  134. SetupDocument (void)
  135. {
  136. Rect    r;
  137.  
  138.     if (SkelQuery (skelQHasColorQD))
  139.         wind = GetNewCWindow (docWindRes, nil, (WindowPtr) -1L);
  140.     else
  141.         wind = GetNewWindow (docWindRes, nil, (WindowPtr) -1L);
  142.     if (wind == (WindowPtr) nil)
  143.     {
  144.         SysBeep (1);
  145.         return;
  146.     }
  147.     SkelWindow (wind,
  148.                 Mouse,
  149.                 Key,
  150.                 Update,
  151.                 Activate,
  152.                 nil,            /* no close box, so no close handler */
  153.                 Clobber,
  154.                 nil,            /* no idle handler */
  155.                 true);            /* irrelevant since no idle handler */
  156.  
  157.     SetRect (&r, 10, 20, 80, 40);
  158.     cancelBtn = NewControl (wind, &r, "\pCancel", true, 0, 0, 1, pushButProc, 0L);
  159.     OffsetRect (&r, 80, 0);
  160.     okBtn = NewControl (wind, &r, "\pOK", true, 0, 0, 1, pushButProc, 0L);
  161.  
  162.     ShowWindow (wind);
  163.     SkelDoEvents (activMask + updateMask);
  164. }
  165.